package com.example.mobileeducationtv;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ReceiveSMSActivity extends Activity {
	
	static TextView messageBox;
	static String tag = "messageTest";
	IntentFilter intentFilter;
	LinearLayout.LayoutParams layoutParams;
    LinearLayout linearLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_receive_sms);
		
		intentFilter = new IntentFilter();
		intentFilter.addAction("ReceiveSMSActivity");
		
		registerReceiver(intentReceiver, intentFilter);
		
		//This is supposed to get all messages in existence
		DatabaseHelper db = new DatabaseHelper(this);
		
		List<Messages> existingMessages = db.getAllMessages();
		
		for(Messages message : existingMessages) {
			String dbMessage = message.getMessage();
			String dbSender = message.getSender();
			appendMessage(dbMessage, dbSender);
			//String log = "Message " + message.getMessage() + "Sender " + message.getSender();
			//Log.d("Messages Test", log);
		}
		//Uncode this to add new number
		//db.addStudent(new Student("Android Port", "15555215556"));
		//db.addStudent(new Student("Daryl", "+6590583095"));
		
		//Uncode this to add new Message
		//db.addMessage(new Messages("+97294202", "Hello World, this is my message"));
		/*
		Button studentDb = (Button) findViewById(R.id.studentDb);
		studentDb.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent dbIntent = new Intent(ReceiveSMSActivity.this, StudentDatabase.class);
				startActivity(dbIntent);
			}
		});
		
		Button messageDb = (Button) findViewById(R.id.messageDb);
		messageDb.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent dbIntent = new Intent(ReceiveSMSActivity.this, MessageDatabase.class);
				startActivity(dbIntent);
			}
		});
		*/
	}
	
	private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			final String msg = intent.getExtras().getString("msg");
			final String num = intent.getExtras().getString("num");
			
			appendMessage(msg, num);
		}
	};
	
	public void appendMessage(final String msg, String num) {
		TableLayout tl = (TableLayout)findViewById(R.id.receiveSMSPage);
		TableRow tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
        TextView messageView = new TextView(ReceiveSMSActivity.this);
        messageView.setWidth(300);
        Button broadcastMessage = new Button(ReceiveSMSActivity.this);
        broadcastMessage.setGravity(Gravity.RIGHT);
        
      //Check the name exists
      		DatabaseHelper db = new DatabaseHelper(this);
      		Student student = new Student(num);
      		//String name;
      		try {
      			student = db.getName(num);
      		} catch(RuntimeException e) {
      			student.setName(num);
      		}
      		String name = student.getName();
      		
        messageView.setText(Html.fromHtml("<b>" + name + "</b>" + "<br>" + "<font color=grey>" + msg + "</font>"));
        broadcastMessage.setText("Broadcast");
        final String messageSender = name;
        final String number = num;
        messageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	Intent replyIntent = new Intent(ReceiveSMSActivity.this, ReplySMSActivity.class);
        		replyIntent.putExtra("message", msg);
        		replyIntent.putExtra("number", number);
        		startActivity(replyIntent);
            }
        });
        tr.addView(messageView);
        tr.addView(broadcastMessage);
        broadcastMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	Intent broadcastIntent = new Intent(ReceiveSMSActivity.this, BroadcastSMSActivity.class);
            	broadcastIntent.putExtra("message", msg);
            	broadcastIntent.putExtra("number", messageSender);
        		startActivity(broadcastIntent);
            }
        });
        tl.addView(tr,new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
	}
	
	public static int AddTwoNumbers(int a, int b) {
		return a+b;
	}
	
	protected void onDestroy() {
		super.onDestroy();
		
		unregisterReceiver(intentReceiver);
	}
}
